package com.wheelpicker.picker.nolinkpicker;

import android.content.Context;
import android.support.annotation.IntRange;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import com.wheelpicker.R;
import com.wheelpicker.adapters.ArrayWheelAdapter;
import com.wheelpicker.base.WheelType;
import com.wheelpicker.utils.Utils;
import com.wheelpicker.wheel.WheelView;

/**
 * Created by jzxiang on 16/4/20.
 */
public class NoLinkWheel {
    private Context mContext;
    private WheelView first, second, third, fourth;
    private ArrayWheelAdapter firstAdapter, secondAdapter, thirdAdapter, fourthAdapter;
    private NoLinkPickerConfig mPickerConfig;


    public NoLinkWheel(View view, NoLinkPickerConfig pickerConfig) {
        mPickerConfig = pickerConfig;
        mContext = view.getContext();
        initialize(view);
    }

    public void initialize(View view) {
        initView(view);
        initFirst();
        initSecond();
        initThird();
        initFourth();
    }


    void initView(View view) {
        LinearLayout ll_container = (LinearLayout) view.findViewById(R.id.ll_container);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View child = inflater.inflate(R.layout.layout_no_link_picker, null, false);
        ll_container.addView(child);
        first = (WheelView) child.findViewById(R.id.first);
        second = (WheelView) child.findViewById(R.id.second);
        third = (WheelView) child.findViewById(R.id.third);
        fourth = (WheelView) child.findViewById(R.id.fourth);

        switch (mPickerConfig.mType) {
            case WheelType.NoLinkType.ALL:

                break;
            case WheelType.NoLinkType.THREE:
                Utils.hideViews(fourth);
                break;
            case WheelType.NoLinkType.TWO:
                Utils.hideViews(third, fourth);
                break;
            case WheelType.NoLinkType.ONE:
                Utils.hideViews(second, third, fourth);
                break;
        }
    }

    private void initFirst() {
        if (mPickerConfig.pickerBeanList == null)
            return;
        firstAdapter = new ArrayWheelAdapter<>(mContext, mPickerConfig.pickerBeanList.get(0));
        firstAdapter.setConfig(mPickerConfig);
        first.setViewAdapter(firstAdapter);
        first.setCurrentItem(0, false);
    }

    private void initSecond() {
        if (second.getVisibility() == View.GONE || mPickerConfig.pickerBeanList == null || mPickerConfig.pickerBeanList.get(1) == null)
            return;
        secondAdapter = new ArrayWheelAdapter(mContext, mPickerConfig.pickerBeanList.get(1));
        secondAdapter.setConfig(mPickerConfig);
        second.setViewAdapter(secondAdapter);
        second.setCurrentItem(0, false);
    }

    private void initThird() {
        if (second.getVisibility() == View.GONE || mPickerConfig.pickerBeanList == null || mPickerConfig.pickerBeanList.get(2) == null)
            return;
        thirdAdapter = new ArrayWheelAdapter(mContext, mPickerConfig.pickerBeanList.get(2));
        thirdAdapter.setConfig(mPickerConfig);
        third.setViewAdapter(thirdAdapter);
        third.setCurrentItem(0, false);
    }

    private void initFourth() {
        if (second.getVisibility() == View.GONE || mPickerConfig.pickerBeanList == null || mPickerConfig.pickerBeanList.get(3) == null)
            return;
        fourthAdapter = new ArrayWheelAdapter(mContext, mPickerConfig.pickerBeanList.get(3));
        fourthAdapter.setConfig(mPickerConfig);
        fourth.setViewAdapter(fourthAdapter);
        fourth.setCurrentItem(0, false);
    }


    public List<SinglePickerBean> getCurrentPickerBean() {
        if (mPickerConfig.pickerBeanList == null)
            return null;
        List<SinglePickerBean> result = new ArrayList<>();
        switch (mPickerConfig.mType) {
            case WheelType.NoLinkType.ALL:
                getPickerBean(0);
                result.add(getPickerBean(0));
                result.add(getPickerBean(1));
                result.add(getPickerBean(2));
                result.add(getPickerBean(3));
                break;
            case WheelType.NoLinkType.THREE:
                result.add(getPickerBean(0));
                result.add(getPickerBean(1));
                result.add(getPickerBean(2));
                break;
            case WheelType.NoLinkType.TWO:
                result.add(getPickerBean(0));
                result.add(getPickerBean(1));
                break;
            case WheelType.NoLinkType.ONE:
                result.add(getPickerBean(0));
                break;
        }
        return result;
    }

    public SinglePickerBean getPickerBean(@IntRange(from = 0, to = 3) int index) {
        List<SinglePickerBean> pickerBean = mPickerConfig.pickerBeanList.get(index);
        if (pickerBean == null) return null;
        switch (index) {
            case 0:
                return pickerBean.get(first.getCurrentItem());
            case 1:
                return pickerBean.get(second.getCurrentItem());
            case 2:
                return pickerBean.get(third.getCurrentItem());
            case 3:
                return pickerBean.get(fourth.getCurrentItem());
        }
        return null;
    }
}
